// ignore_for_file: must_be_immutable

/*
 * @Author: CodeHare
 * @Date: 2023-11-22 18:02:22
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-14 10:09:16
 * @Description: 新增报障信息
 */
import 'package:get/get.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:sprintf/sprintf.dart';
import 'package:yt_devops/components/card_wrap.dart';
import 'package:yt_devops/pages/common/select_page/select_page_model.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/model/amap_poi.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yt_devops/components/select_phone_widget.dart';
import 'package:yt_devops/pages/site/site_list/site_list_model.dart';
import 'package:yt_devops/components/yt_form/widgets/form_text_field.dart';
import 'package:yt_devops/components/yt_form/widgets/reactive_text_select.dart';
import 'package:yt_devops/components/yt_form/widgets/reactive_bruno_input_text.dart';
import 'package:yt_devops/pages/tasks/my_fault_report/create_fault_report/create_fault_report_controller.dart';

class CreateFaultReportPage extends GetView<CreateFaultReportController> {
  CreateFaultReportPage({Key? key}) : super(key: key);

  BrnFormItemConfig theme = BrnFormItemConfig(
    formPadding:
        EdgeInsets.only(left: 6.w, right: 16.w, top: 17.h, bottom: 17.h),
    titlePaddingLg: EdgeInsets.only(left: 10.w),
    disableTextStyle: BrnTextStyle(
      color: Colour.mainTextColor,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(sprintf(
            ResKey.commonCreateSentence.tr, [ResKey.workOrderFaultOrder.tr])),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colour.black),
          onPressed: () => Get.back(),
        ),
      ),
      body: GetBuilder<CreateFaultReportController>(
        builder: (controller) => ReactiveFormBuilder(
          onWillPop: () async {
            if (FocusScope.of(context).hasFocus) {
              FocusScope.of(context).unfocus;
            }
            return true;
          },
          form: controller.buildForm,
          builder: (context, form, child) {
            controller.form = form;

            return ListView(
              children: <Widget>[
                CardWrap(
                  padding: EdgeInsets.only(left: 7.w, right: 16.w),
                  child: Column(
                    children: <Widget>[
                      /// 站点名称
                      ReactiveTextSelect<SiteModel, SiteModel>(
                        formControlName: 'site',
                        title: ResKey.siteName.tr,
                        value: (site) => site?.name ?? '',
                        validationMessages: {
                          ValidationMessage.required: (_) =>
                              ResKey.selectSite.tr
                        },
                        requireStar: true,
                        selectPageProp: SelectPageProp(
                          ResKey.site.tr,
                          getIdByKey: (site) => site.siteId,
                          getNameKey: (site) => site.name ?? '',
                          isSearch: true,
                          initSelectValues: form.controls['site']?.value == null
                              ? []
                              : [form.controls['site']?.value as SiteModel],
                          service: (params) =>
                              controller.siteServices.getSitesList({
                            'current': params.pageNum,
                            'pageSize': params.pageSize,
                            'status': 2, // 2-全部站点
                            ...params.extra ?? {}
                          }),
                        ),
                        onChange: () {
                          SiteModel? site =
                              form.controls['site']?.value as SiteModel?;
                          if (site != null) {
                            controller.update();
                          }
                        },
                      ),

                      /// 故障标题
                      FormTextField(
                        formControlName: 'name',
                        validationMessages: {
                          ValidationMessage.required: (_) => ResKey.faultTips.tr
                        },
                        requireStar: true,
                        style: TextStyles.f16,
                        title: ResKey.failureTitle.tr,
                      ),
                    ],
                  ),
                ),

                /// 问题描述
                CardWrap(
                  padding: EdgeInsets.only(left: 7.w, right: 16.w),
                  child: ReactiveBrunoTextInput(
                    requiredStar: true,
                    formControlName: 'content',
                    validationMessages: {
                      ValidationMessage.required: (_) =>
                          ResKey.problemStasticTips.tr,
                    },
                    title: ResKey.problemStastic.tr,
                    autoFocus: false,
                    padding: EdgeInsets.only(left: 12.w),
                  ),
                ),

                /// 附件图片
                ReactiveFormConsumer(
                  builder: (context, formGroup, child) => CardWrap(
                    child: SelectPhotoWidget(
                        backgroundColor: Colors.transparent,
                        form.controls['descPicUrl']!.value as List<String>,
                        true,
                        3,
                        decString: ResKey.siteBaseInfoLimitPictures3.tr,
                        titleString: ResKey.imageAttachment.tr,
                        (List<String> list) {
                      controller.update();
                    }),
                  ),
                ),

                ReactiveFormConsumer(
                  builder: (context, form, child) => SafeArea(
                    child: Container(
                      color: Colour.white,
                      margin: const EdgeInsets.only(top: 10),
                      padding:
                          EdgeInsets.symmetric(horizontal: 16.w, vertical: 16),
                      child: YtButton(
                        enabled: form.valid,
                        text: ResKey.submit.tr,
                        onPress: () {
                          controller.commit(form);
                        },
                      ),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  Widget borderWidget(Widget child) {
    return Container(
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Colour.dividerColor,
          ),
        ),
      ),
      child: child,
    );
  }
}
